package cn.edu.pku.ss.situationawareperceiver;

import java.util.ArrayList;

import android.util.Log;
import cn.edu.pku.ss.situationawareperceiver.ContextConstants.ContextType;

public class ContextInterpreter {
	/**
	 * Module: ContextInterpreter Responsibilities: 1. Process interpret request
	 * from other modules(ContextSharingRequest/ContextSharingService/SRA); 2.
	 * Transfer the result to SRA or use it in building ContextSharingRequest;
	 * 3. Mapping from (context type + truth value) to situation and vice versa;
	 **/

	private static ArrayList<Integer> verifiedSituationIdsList = new ArrayList<Integer>();

	private static final String TAG = "ContextInterpreter";

	public static void interpretContextToSituation(ContextType contextType,
			String truthValue) {
		/*
		 * This method works for interpret context into situation and transfer
		 * the result to SRA.
		 */
		logInfo("interpretContextToSituation: got a new job of interpreting "
				+ contextType.name() + " - " + truthValue);
		switch (contextType) {
		case FLOOR_WEIGHT:
			if (Integer.parseInt(truthValue) > 200) {
				SituationReasoner.getSituation(1).setVerified(true);
				SituationReasoner.getSituation(1).setExistence(true);
				verifiedSituationIdsList.add(1);
			} else {
				SituationReasoner.getSituation(1).setVerified(true);
				SituationReasoner.getSituation(1).setExistence(false);
			}
			break;
		case HAS_COFFEE_CUP:
			if (truthValue.equals("true")) {
				SituationReasoner.getSituation(2).setVerified(true);
				SituationReasoner.getSituation(2).setExistence(true);
				verifiedSituationIdsList.add(2);
			} else if (truthValue.equals("false")) {
				SituationReasoner.getSituation(2).setVerified(true);
				SituationReasoner.getSituation(2).setExistence(false);
			}
			break;
		case COFFEE_TEMPERATURE:
			if (Integer.parseInt(truthValue) > 50) {
				SituationReasoner.getSituation(3).setVerified(true);
				SituationReasoner.getSituation(3).setExistence(true);
				verifiedSituationIdsList.add(3);
			} else {
				SituationReasoner.getSituation(3).setVerified(true);
				SituationReasoner.getSituation(3).setExistence(false);
			}
			break;
		case IS_MEMBER:
			if (truthValue.equals("true")) {
				SituationReasoner.getSituation(4).setVerified(true);
				SituationReasoner.getSituation(4).setExistence(true);
				verifiedSituationIdsList.add(4);
			} else if (truthValue.equals("false")) {
				SituationReasoner.getSituation(4).setVerified(true);
				SituationReasoner.getSituation(4).setExistence(false);
			}
			break;
		case LOCATION:
			if (truthValue.equals("meeting room")) {
				SituationReasoner.getSituation(7).setVerified(true);
				SituationReasoner.getSituation(7).setExistence(true);
				verifiedSituationIdsList.add(5);
			} else {
				SituationReasoner.getSituation(7).setVerified(true);
				SituationReasoner.getSituation(7).setExistence(false);
			}
			break;
		case NUMBER_OF_PEOPLE_IN_ROOM:
			if (Integer.parseInt(truthValue) > 1) {
				SituationReasoner.getSituation(8).setVerified(true);
				SituationReasoner.getSituation(8).setExistence(true);
				verifiedSituationIdsList.add(8);
				if (Integer.parseInt(truthValue) > 3) {
					SituationReasoner.getSituation(29).setVerified(true);
					SituationReasoner.getSituation(29).setExistence(true);
					verifiedSituationIdsList.add(29);
				} else {
					SituationReasoner.getSituation(29).setVerified(true);
					SituationReasoner.getSituation(29).setExistence(false);
				}
			} else {
				SituationReasoner.getSituation(8).setVerified(true);
				SituationReasoner.getSituation(8).setExistence(false);
				SituationReasoner.getSituation(29).setVerified(true);
				SituationReasoner.getSituation(29).setExistence(false);
			}
			break;
		case DIARY:
			if (truthValue.equals("meeting")) {
				SituationReasoner.getSituation(5).setVerified(true);
				SituationReasoner.getSituation(5).setExistence(true);
				verifiedSituationIdsList.add(5);
			} else {
				SituationReasoner.getSituation(5).setVerified(true);
				SituationReasoner.getSituation(5).setExistence(false);
			}
			break;
		case ROOM_LIGHT:
			if (truthValue.equals("on")) {
				SituationReasoner.getSituation(9).setVerified(true);
				SituationReasoner.getSituation(9).setExistence(true);
				verifiedSituationIdsList.add(9);
			} else if (truthValue.equals("off")) {
				SituationReasoner.getSituation(9).setVerified(true);
				SituationReasoner.getSituation(9).setExistence(false);
			}
			break;
		case PROJECTOR:
			if (truthValue.equals("on")) {
				SituationReasoner.getSituation(10).setVerified(true);
				SituationReasoner.getSituation(10).setExistence(true);
				verifiedSituationIdsList.add(10);
			} else if (truthValue.equals("off")) {
				SituationReasoner.getSituation(10).setVerified(true);
				SituationReasoner.getSituation(10).setExistence(false);
			}
			break;
		case SOUND_METER:
			if (Integer.parseInt(truthValue) > 5) {
				SituationReasoner.getSituation(17).setVerified(true);
				SituationReasoner.getSituation(17).setExistence(true);
				verifiedSituationIdsList.add(17);
			} else {
				SituationReasoner.getSituation(17).setVerified(true);
				SituationReasoner.getSituation(17).setExistence(false);
			}
			break;
		case SOON_TO_DINNER:
			if (truthValue.equals("true")) {
				SituationReasoner.getSituation(42).setVerified(true);
				SituationReasoner.getSituation(42).setExistence(true);
				verifiedSituationIdsList.add(42);
			} else if (truthValue.equals("false")) {
				SituationReasoner.getSituation(42).setVerified(true);
				SituationReasoner.getSituation(42).setExistence(false);
			}
			break;
		case IN_BED:
			if (truthValue.equals("true")) {
				SituationReasoner.getSituation(38).setVerified(true);
				SituationReasoner.getSituation(38).setExistence(true);
				verifiedSituationIdsList.add(38);
			} else if (truthValue.equals("false")) {
				SituationReasoner.getSituation(38).setVerified(true);
				SituationReasoner.getSituation(38).setExistence(false);
			}
			break;
		case AT_NIGHT:
			if (truthValue.equals("true")) {
				SituationReasoner.getSituation(39).setVerified(true);
				SituationReasoner.getSituation(39).setExistence(true);
				verifiedSituationIdsList.add(39);
			} else if (truthValue.equals("false")) {
				SituationReasoner.getSituation(39).setVerified(true);
				SituationReasoner.getSituation(39).setExistence(false);
			}
			break;
		case HOLD_READING_EQUIPMENT:
			if (truthValue.equals("true")) {
				SituationReasoner.getSituation(33).setVerified(true);
				SituationReasoner.getSituation(33).setExistence(true);
				verifiedSituationIdsList.add(33);
			} else if (truthValue.equals("false")) {
				SituationReasoner.getSituation(33).setVerified(true);
				SituationReasoner.getSituation(33).setExistence(false);
			}
			break;
		case TV:
			if (truthValue.equals("on")) {
				SituationReasoner.getSituation(30).setVerified(true);
				SituationReasoner.getSituation(30).setExistence(true);
				verifiedSituationIdsList.add(30);
			} else if (truthValue.equals("off")) {
				SituationReasoner.getSituation(30).setVerified(true);
				SituationReasoner.getSituation(30).setExistence(false);
			}
			break;
		case IN_SEAT:
			if (truthValue.equals("true")) {
				SituationReasoner.getSituation(31).setVerified(true);
				SituationReasoner.getSituation(31).setExistence(true);
				verifiedSituationIdsList.add(31);
			} else if (truthValue.equals("false")) {
				SituationReasoner.getSituation(31).setVerified(true);
				SituationReasoner.getSituation(31).setExistence(false);
			}
			break;
		case IN_FRIENDS_LIST:
			if (truthValue.equals("true")) {
				SituationReasoner.getSituation(28).setVerified(true);
				SituationReasoner.getSituation(28).setExistence(true);
				verifiedSituationIdsList.add(28);
			} else if (truthValue.equals("false")) {
				SituationReasoner.getSituation(28).setVerified(true);
				SituationReasoner.getSituation(28).setExistence(false);
			}
			break;
		case IN_KITCHEN:
			if (truthValue.equals("true")) {
				SituationReasoner.getSituation(41).setVerified(true);
				SituationReasoner.getSituation(41).setExistence(true);
				verifiedSituationIdsList.add(41);
			} else if (truthValue.equals("false")) {
				SituationReasoner.getSituation(41).setVerified(true);
				SituationReasoner.getSituation(41).setExistence(false);
			}
			break;
		case DIARY_FOR_TRAVEL:
			if (truthValue.equals("true")) {
				SituationReasoner.getSituation(35).setVerified(true);
				SituationReasoner.getSituation(35).setExistence(true);
				verifiedSituationIdsList.add(35);
			} else if (truthValue.equals("false")) {
				SituationReasoner.getSituation(35).setVerified(true);
				SituationReasoner.getSituation(35).setExistence(false);
			}
			break;
		default: {
			Log.e(TAG, "interpretContextToSituation: switch into default.");
			break;
		}
		}

		/* send activate msg to main activity */
		for (Integer i : verifiedSituationIdsList) {
			logInfo("New situation perceived: "
					+ SituationReasoner.getSituation(i).getName());
			SituationReasoner.activateTagOnMainActivity(true, i);
		}
	}

	public static void requestSituation(int situationID) {
		/*
		 * This method works for SRA to interpret a situation and use the result
		 * to build a ContextSharingRequest. (ContextSharingService is in charge
		 * of express the interest over the opportunistic network.)
		 */
		ContextType contextType = ContextType.NONE;
		switch (situationID) {
		case 1:
			contextType = ContextType.FLOOR_WEIGHT;
			break;
		case 2:
			contextType = ContextType.HAS_COFFEE_CUP;
			break;
		case 3:
			contextType = ContextType.COFFEE_TEMPERATURE;
			break;
		case 4:
			contextType = ContextType.IS_MEMBER;
			break;
		case 5:
			contextType = ContextType.DIARY;
			break;
		case 7:
			contextType = ContextType.LOCATION;
			break;
		case 8:
		case 29:
			contextType = ContextType.NUMBER_OF_PEOPLE_IN_ROOM;
			break;
		case 9:
			contextType = ContextType.ROOM_LIGHT;
			break;
		case 10:
			contextType = ContextType.PROJECTOR;
			break;
		case 17:
			contextType = ContextType.SOUND_METER;
			break;
		case 42:
			contextType = ContextType.SOON_TO_DINNER;
			break;
		case 38:
			contextType = ContextType.IN_BED;
			break;
		case 39:
			contextType = ContextType.AT_NIGHT;
			break;
		case 33:
			contextType = ContextType.HOLD_READING_EQUIPMENT;
			break;
		case 30:
			contextType = ContextType.TV;
			break;
		case 31:
			contextType = ContextType.IN_SEAT;
			break;
		case 28:
			contextType = ContextType.IN_FRIENDS_LIST;
			break;
		case 41:
			contextType = ContextType.IN_KITCHEN;
			break;
		case 35:
			contextType = ContextType.DIARY_FOR_TRAVEL;
			break;
		}
		ContextShareManager.getInstance().queryContext(contextType);
		logInfo("ContextInterpreter is quering context [" + contextType.name()
				+ "] for verify situation ["
				+ SituationReasoner.getSituation(situationID).getName() + "].");
	}

	/* Log methods */
	public static void logInfo(String msg) {
		Log.i(TAG, msg);
	}
}
